/*
 * Trace task definition.
 * Each task describes a single process we need to trace for file i/o.
 */

#ifndef FTRACE_TASK_H
#define FTRACE_TASK_H

#include "file.h"

#include <list>


/**
 * Tracing task.
 * Encapsulates context required to trace a single process.
 */
class TraceTask {
public:

	/// Task state tags.
	typedef enum {
		kNotInitialized = 0,	// Task is not initialized for tracing
		kStopped,		// Task is traced and stopped.
		kRunning,		// Task is traced and running.
	} task_state_t;


	TraceTask() : m_pid(0), m_state(kNotInitialized) {};


	/**
 	 * Initialize task by running command with arguments.
 	 * On success transits task state to kTask_Stopped.
 	 * /command		Command to execute.
	 * /argc		Arguments count.
	 * /argv		Arguments array.
	 */
	int start(const char* command, char** argv);


	/**
	 * Initialize task by attaching to running process and stopping it.
	 * On success transits task state to kStopped and registers all files currently opened by traced process.
	 * /pid			Running process id to attach to.
	 */
	int attach(pid_t pid);


	/**
 	 * Start tracing this task.
 	 * Task state should be kStopped to start tracing.
	 */
	int trace();


	/// Get current task state.
	int state() const {
		return m_state;
	}


protected:


	/// Called to set up tracing options for task and transit to kTask_Stopped state.
	void prepare(pid_t pid);

	/// Resume stopped task with specified signal. Default 0 is no signal.
	void resume(int signo = 0);

	/// Process traced task status. Called each time when task is stopped.
	void process_status(int status);

private:

	pid_t 			m_pid;		// Traced process pid.
	task_state_t		m_state;	// Task current state.
	std::list<ftrace_file>	m_freg;		// File register.
};


#endif

